#!/bin/bash -e

source $(dirname $0)/version

cd $(dirname $0)/..

declare -A OS_ARCH_ARG

OS_PLATFORM_ARG=(linux windows darwin)
OS_ARCH_ARG[linux]="amd64 arm"
OS_ARCH_ARG[windows]="386 amd64"
OS_ARCH_ARG[darwin]="amd64"

CGO_ENABLED=0 go build -ldflags="-w -s -X main.VERSION=$VERSION -extldflags -static" -o bin/rancher

if [ -n "$CROSS" ]; then
    rm -rf build/bin
    mkdir -p build/bin
    for OS in ${OS_PLATFORM_ARG[@]}; do
        for ARCH in ${OS_ARCH_ARG[${OS}]}; do
            OUTPUT_BIN="build/bin/rancher_$OS-$ARCH"
            if test "$OS" = "windows"; then
                OUTPUT_BIN="${OUTPUT_BIN}.exe"
            fi
            echo "Building binary for $OS/$ARCH..."
            GOARCH=$ARCH GOOS=$OS CGO_ENABLED=0 go build \
                  -ldflags="-w -X main.VERSION=$VERSION" \
                  -o ${OUTPUT_BIN} ./
        done
    done
fi
